﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace WeiTalk.SinaEntities
{
    public class Utility
    {
        static Regex regex = new Regex(@"<(.|\n)+?>");
        public static string ConvertUTCTimeToLocalTime(string UTCTime)
        {
            if (String.IsNullOrEmpty(UTCTime)) return "";
            string[] cx = UTCTime.Split(' ');
            System.Globalization.DateTimeFormatInfo g = new System.Globalization.DateTimeFormatInfo();
            g.LongDatePattern = "dd MMMM yyyy";
            DateTime DT = DateTime.Parse(string.Format("{0} {1} {2} {3}", cx[2], cx[1], cx[5], cx[3]), g);
            return DateStringFromNow(DT);
        }

        public static string DateStringFromNow(DateTime dt)
        {
            TimeSpan span = DateTime.Now - dt;
            if (span.TotalDays > 60)
            {
                return dt.ToShortDateString();
            }
            else if (span.TotalDays > 30)
            {
                return
                "1个月前";
            }
            else if (span.TotalDays > 14)
            {
                return
                "2周前";
            }
            else if (span.TotalDays > 7)
            {
                return
                "1周前";
            }
            else if (span.TotalDays > 1)
            {
                return
                string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
            }
            else if (span.TotalHours > 1)
            {
                return
                string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
            }
            else if (span.TotalMinutes > 1)
            {
                return
                string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
            }
            else if (span.TotalSeconds >= 1)
            {
                return
                string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
            }
            else
            {
                return "1秒前";
            }
        }

        public static string FormatSource(string source)
        {
            return regex.Replace(source, "");
        }
    }
}
